#!/usr/bin/env python
# -*-coding: utf8 -*-
'''
# created by: iori
# created on: Wed 11 Dec 2013 09:44:54 PM CST
'''
import time
import os,sys, traceback
from xunlei import get_vod


def upload(folder):
    fs = sorted(os.listdir(folder))
    if not fs: return # empty folder
    vod = get_vod()
    while True:
        if not fs: break
        f = fs[0]
        if f.endswith('torrent'):
            try:
                print vod.add_bt(os.path.join(folder, f))
                os.remove(os.path.join(folder, f))
                fs.remove(f)
            except Exception, e:
                print f, ' :fail', e
                traceback.print_exc(file=sys.stdout)
                vod = get_vod()
            time.sleep(5)

def main():
    folder = 'new'
    for f in sorted(os.listdir(folder)):
        if f.endswith('torrent'):
            print '[{}]'.format(f)
            try:
                vod = get_vod()
                vod.add_bt(os.path.join(folder, f))
            except Exception, e:
                print e
                with open('undo.txt', 'a') as fp:
                    fp.write(f)

            time.sleep(5)
if __name__ == '__main__':
    upload(sys.argv[1])
